import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class pull {
    public static void main(String[] args) {
        // 要下载的文件名与地址
        String objectName = "A2290934BF2AC38B70216491CD38E229.jpg";
        String downloadFilePath = "C:\\Users\\李进峰\\Pictures\\pull\\" + objectName;








        String endpoint = "oss-rg-china-mainland.aliyuncs.com";
        String accessKeyId = "LTAI5tKFf9cjiA8hzS8Sq8Cu";
        String accessKeySecret = "DFIaG9jTtuKrM9pvda7T0TBT2NQrBt";
        String bucketName = "rsl-plus";

        OSS ossClient = new OSSClientBuilder()
                .build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 获取 OSSObject（包含输入流）
            OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, objectName));
            InputStream inputStream = ossObject.getObjectContent();

            // 将输入流写入到本地文件
            FileOutputStream fileOutputStream = new FileOutputStream(new File(downloadFilePath));
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }

            inputStream.close();
            fileOutputStream.close();
            System.out.println("文件下载成功，保存到：" + downloadFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ossClient.shutdown();
        }
    }
}
